package com.mybatis.panel;

import com.mybatis.entity.User;
import com.mybatis.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class LoginPanel extends JPanel {

    public static int pageSize = 1;

    public static String dataRow;

    public LoginPanel() {
        setLayout(null);
        setBounds(0, 0, 300, 300);

        //===========================================
//        JPanel panel = new JPanel(new BorderLayout());
//        panel.setBounds(0, 0, 900, 600);//位置
//        panel.add(new JLabel("用户名"));//文字


        JPanel panel2 = new JPanel();
        panel2.setBounds(0, 140, 0, 0);
        add(panel2);

        JLabel jLabel3 = new JLabel();
        jLabel3.setText("用户名:");
        jLabel3.setBounds(50, 50, 50, 50);
        add(jLabel3);

        JLabel jLabel4 = new JLabel();
        jLabel4.setBounds(62, 75, 50, 50);
        jLabel4.setText("密码:");


        //文本框
        JTextField jTextField = new JTextField(10);
        jTextField.setBounds(100, 65, 100, 20);
        add(jTextField);

        add(jLabel4);

        JPasswordField passwordField = new JPasswordField(10);
        passwordField.setBounds(100, 90, 100, 20);
        add(passwordField);

        ButtonGroup group = new ButtonGroup();

        JRadioButton radioButton1 = new JRadioButton("管理员", false);
        radioButton1.setBounds(70, 120, 20, 20);
        add(radioButton1);

//        JButton loginButton2 = new JButton("左方点击管理员");
//        //loginButton.setText("www");
//        loginButton2.setBounds(90, 120, 40, 30);
//        loginButton2.setMargin(new Insets(0, 0, 0, 0));
//        add(loginButton2, BorderLayout.WEST);
//        JRadioButton radioButton2 = new JRadioButton("普通用户",true);
//        radioButton1.setBounds(60, 120, 20, 20);
        //group.add(radioButton1);
//        JLabel usertype = new JLabel("www");
//        usertype.setText("管理员");
//        usertype.setBounds(90, 120, 20, 20);
//        add(usertype);


        JButton loginButton2 = new JButton("<--管理员");
        //loginButton.setText("www");
        loginButton2.setBounds(100, 120, 80, 20);
        loginButton2.setMargin(new Insets(0, 0, 0, 0));
        add(loginButton2);


        JButton loginButton = new JButton("登陆");
        //loginButton.setText("www");
        loginButton.setBounds(70, 150, 40, 30);
        loginButton.setMargin(new Insets(0, 0, 0, 0));
        add(loginButton);

        JButton jButton = new JButton("注册");
        jButton.setBounds(140, 150, 40, 30);
        jButton.setMargin(new Insets(0, 0, 0, 0));
        //panel2.add(jButton,BorderLayout.EAST);
        add(jButton);

        final int[] type = {0};

        radioButton1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (radioButton1.isSelected()) {
                    type[0] = 1;
                }else type[0] = 0;

            }
        });


        loginButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                String text = jTextField.getText();
                char[] password = passwordField.getPassword();
                if (text == null || text.length() <= 0 || password == null || password.length <= 0) {
                    JOptionPane.showMessageDialog(panel2,
                            "用户名或密码不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                User user = new User();
                user.setName(text);
                user.setPassword(String.valueOf(password));
                user.setType(type[0]);
                //System.out.println(user);
                User login = UserService.Login(user);
                if (login == null) {
                    JOptionPane.showMessageDialog(panel2,
                            "用户名或密码错误!", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                Integer type = UserService.getType(user);
                //Integer type = user.getType();
                MainJFrame.userType = type;
                JOptionPane.showMessageDialog(panel2,
                        "登陆成功", "系统提示", JOptionPane.WARNING_MESSAGE);
                MainJFrame.jFrame.setSize(400, 300);
                setPanel(new SelectPanel());
                //setPanel(new StudentPanel());

                //new Thread(()->{
//                    JFrame frame = new JFrame();
//                    frame.setVisible(true);
//                    frame.setSize(400,300);
//                    System.out.println("hello");
                //}).start();


            }
        });


        jButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String text = jTextField.getText();
                char[] password = passwordField.getPassword();
                if (text == null || text.length() <= 0 || password == null || password.length <= 0) {
                    JOptionPane.showMessageDialog(panel2,
                            "用户名或密码不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                User user = new User();
                user.setName(text);
                user.setPassword(String.valueOf(password));
                Integer integer = UserService.getAll();
                user.setId(++integer);
                //System.out.println(user);
                user.setType(type[0]);
                User aNull = UserService.getNull(user);
                if (aNull != null) {
                    JOptionPane.showMessageDialog(panel2,
                            "用户名已存在!", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                UserService.insert(user);
                JOptionPane.showMessageDialog(panel2,
                        "注册成功", "系统提示", JOptionPane.WARNING_MESSAGE);
                //setPanel(new StudentPanel());
            }
        });

    }

    public void setPanel(int page) {
        pageSize = page;
        MainJFrame.setPanel(new LoginPanel());
    }

    public void setPanel(JPanel panel) {
        MainJFrame.setPanel(panel);
    }

}
